import LoginStore from '../login/store.js';
import { reqHeader, proxy} from '../../utils/util.js';
const defaultData = {
  imgUrls: [],
  urlStr: "",
  params: {},
  hasNext: true,
  selectedID: ""
}
const Swiper={
  data: defaultData,
  resetData:function(){
    Object.assign(this.data, defaultData);
  },
  setData:function(obj){
    Object.assign(this.data, obj);
  },
  nextPage:function(callback){
    this.data.params.page++;
    this.loadData(callback);
  },
  loadData: function (callback){
    let me = this;
    let data = this.data;
    let userAccountInfo = LoginStore.userInfo || {};
    wx.request({
      url: data.urlStr,
      data: Object.assign(data.params,{
        access_token: userAccountInfo.access_token
      }),
      header: reqHeader,
      success: function (res) {
        if (res.statusCode != 200){
          return;
        }
        let successFun = ()=>{
          me.setData({ hasNext: true })
          if (!res.data.length || res.data.length < (data.params.size || 20)) {
            me.setData({ hasNext: false })
          }
          let filterArray = [];
          res.data.map((item)=>{
            if(item.resourceType !== 3){
              filterArray.push(item);
            }
          })
          let listData = data.imgUrls.concat(filterArray);
          me.setData({ imgUrls: listData });
          if (callback && callback instanceof Function) {
            callback(listData);
          }
        }
        if(res.data && res.data instanceof Array){
          successFun();
        }else{
          res = res.data;
          if (res.status == 200) {
            successFun();
          } else {
            console.log(res.message);
          }
        }
        
      }
    });
  }

}
export default Swiper